<template>
  <div style="padding: 15px;">
    <vxe-toolbar id="warehouseLocationId" :custom="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button v-if="!shopAudit" @click="modelShowHide('add')">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="600"
      align="center"
      :loading="loading"
      :export-config='{type:"csv"}'
      :data="tableData"
      :edit-config="{trigger: 'click', mode: 'row', showStatus: true}"
      sort-config.remote
      @sort-change="sortList"
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" width="60"></vxe-table-column>
      <vxe-table-column type="checkbox" width="60"></vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="status" title="审核状态" min-width="150" sortable show-header-overflow show-overflow :formatter="formatterStatus"/>
        <template #header>
          <el-select v-model="listQuery.status" placeholder="请选择状态" clearable>
            <el-option value="0" label="未审核"/>
            <el-option value="1" label="审核通过"/>
            <el-option value="2" label="审核未通过"/>
          </el-select>
        </template>
      </vxe-table-column>
      <!-- shipmentName -->
      <vxe-table-column>
        <vxe-table-column field="shipmentName" title="入驻商" min-width="150" sortable />
        <template #header>
          <el-input v-model="listQuery.shipmentName " placeholder="入驻商" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="carrierTypeName" title="承运商类型" min-width="150" sortable />
        <template #header>
          <el-select v-model="listQuery.carrierType" placeholder="承运商类型" clearable>
            <el-option v-for="item in carrierTypeList" :key="item.code" :label="item.value" :value="item.code" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="carrierCode" title="承运商编码" min-width="150" sortable/>
        <template #header>
          <el-input v-model="listQuery.carrierCode " placeholder="承运商编码" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="name" title="承运商名称" min-width="150" sortable/>
        <template #header>
          <el-input v-model="listQuery.name " placeholder="承运商名称" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="platformType"
          title="平台类型"
          min-width="150"
          sortable
          show-header-overflow
          show-overflow
          :formatter="({ cellValue }) => this.getEnumsOrDict('dict', this.platformTypeList, ['code', 'name']).get(cellValue)"
        />
        <template #header>
          <el-select v-model="listQuery.platformType" placeholder="请选择" clearable>
            <el-option
              v-for="item in platformTypeList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="shopName" title="关联店铺" min-width="150" sortable/>
        <template #header>
          <el-input v-model="listQuery.shopName " placeholder="请输入店铺名" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="createdName" title="创建人" min-width="150" sortable/>
        <template #header>
          <el-input v-model="listQuery.createdName" placeholder="创建人" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          show-header-overflow
          show-overflow
          title="创建时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="modifiedName" title="修改人" min-width="150" sortable/>
        <template #header>
          <el-input v-model="listQuery.modifiedName" placeholder="请输入修改人" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtModified"
          show-header-overflow
          show-overflow
          title="修改时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtModifiedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtModified')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" min-width="150" v-if="!shopAudit">
        <template slot-scope="scope">
          <el-tooltip  effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="modelShowHide('detail', scope.row.code)">
              <i class="el-icon-document"/>
            </vxe-button>
          </el-tooltip>
          <el-tooltip  effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="modelShowHide('edit', scope.row.code)">
              <i class="el-icon-edit"/>
            </vxe-button>
          </el-tooltip>
          <el-tooltip  effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete"/>
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" min-width="150" v-if="shopAudit">
        <template slot-scope="scope">
          <el-tooltip  effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="modelShowHide('detail', scope.row.code)">
              <i class="el-icon-document"/>
            </vxe-button>
          </el-tooltip>
          <el-tooltip  effect="dark" content="审核" placement="top"  v-if="scope.row.status == '0'">
            <vxe-button type="text" @click="modelShowHide('audit', scope.row.code)">
              <i class="el-icon-s-check"/>
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
    <vxe-modal v-model="operationModel" :title="operation.title" width="820" min-height="600" destroy-on-close>
      <compEdit :operationType="operation.type" :operationCode="operationCode" @operationComplete="modelShowHide();findList()"/>
    </vxe-modal>
  </div>
</template>
<script>
  import Vue from 'vue'
  import 'xe-utils'
  import VXETable from 'vxe-table'
  import 'vxe-table/lib/index.css'
  import XEUtils from 'xe-utils'
  import VXEUtils from 'vxe-utils'
  Vue.use(VXETable, VXEUtils, XEUtils, {mounts: ['cookie']})
  import compEdit from './components/carrierEdit.vue'
  import { carrierList, carrierDel } from '@/api/basicInfo'
  import { getListDict } from '@/api/commonApi';
  export default {
    name: '承运商审核',
    components: {
      compEdit,
    },
    data() {
      return {
        loading: false,
        tableData:[],
        gmtCreatedRange:"",
        gmtModifiedRange:"",
        pickerOptions: {
          shortcuts: [
            {
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              }
            }
          ]
        },
        listQuery: {
          shipmentName:'',
          carrierTypeName:'',
          name:'',
          businessLicenseImgNumber:'',
          legalPerson:'',
          idCard:'',
          taxPoint:'',
          merchantsName:'',
          address:'',
          isSettlement:'',
          remark:'',
          pageNum: 1,
          pageSize: 10,
          gmtCreated:'',        //创建时间
          gmtModified:'',       //修改时间
          createdName:''
        },
        operationModel: false,
        operation: {
          add: '添加',
          edit: '编辑',
          detail: '详情',
          audit: '审核',
          title: '添加',
          type: 'add',
        },
        operationCode: '', // 修改，详情，数据的id
        shopAudit: false,
        auditStatus: ['未审核', '审核通过', '审核未通过'],
        carrierTypeList: [
          {code: "express", value: "快递"},
          {code: "logistics", value: "物流"}
        ],
        platformTypeList: [] // 平台类型
      }
    },
    async created() {
      if ( this.$route.path === '/approveManager/carrierAudit' ) {
        this.shopAudit = true
      }
      // 获取枚举数据，平台类型列表
      await getListDict({ types: 'carrierPlatform' }).then(res => {
        this.platformTypeList = res.data[0].dictList
      })
      this.findList()
    },
    methods: {
      modelShowHide(type, code) {
        this.operationModel = !this.operationModel
        if (type) {
          this.operation.type = type
          this.operation.title = this.operation[type]
        }
        if (code) {
          this.operationCode = code
        }
      },
      changeDate(value, prefix) {
          if (value) {
              this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
              this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
          } else {
              this.listQuery[`${prefix}Start`] = "";
              this.listQuery[`${prefix}End`] = "";
          }
      },
      // 翻译审核状态
      formatterStatus({cellValue}) {
        return this.auditStatus[cellValue] || ''
      },
      //获取列表
      findList() {
        this.loading = true
        return new Promise(resolve=>{
          carrierList(this.listQuery).then(res=>{
            if(res.status === 0){
              this.tableData = res.data.records
              this.listQuery.total = res.data.total
            }
            this.loading = false
          })
          resolve()
        })
      },
      // 排序
      sortList({column, property, order}) {
        if (order) {
          this.listQuery.sortField = property
          this.listQuery.sortType = order
          if(property === 'carrierTypeName'){
            this.listQuery.sortField = 'carrierType'
          }
        } else {
          this.listQuery.sortField = ''
        }
        this.findList()
      },
      // 删除方法
      funcDelete(row) {
        this.$confirm('确认删除吗？').then(_ => {
            carrierDel(row).then(res=>{
            if(res.status === 0){
              this.$message.success('删除成功');
              this.findList()
            }
        })
        }).catch(_ => {})

      },
      // 分页handle
      handlePageChange({currentPage, pageSize}) {
        this.listQuery.pageNum = currentPage
        this.listQuery.pageSize = pageSize
        this.findList()
      },
      // 设置底部合计
      footerMethod({columns, data}) {
        return [
          columns.map((column, columnIndex) => {
            if (columnIndex === 0) {
              return '合计'
            }
            if (['pageviews', 'forecast'].includes(column.property)) {
              return XEUtils.sum(data, column.property)
            }
            return null
          })
        ]
      }
    }
  }
</script>

<style>
  .vxe-table .vxe-body--column:not(.col--ellipsis), .vxe-table .vxe-footer--column:not(.col--ellipsis), .vxe-table .vxe-header--column:not(.col--ellipsis) {
    padding: 6px 0;
  }

  .vxe-toolbar {
    padding: 0 0 10px 0
  }
</style>
